package cn.mxh.pethspt.dao.impl;

import cn.mxh.pethspt.dao.DingDao;
import cn.mxh.pethspt.entity.Ding;
import cn.mxh.pethspt.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.util.ArrayList;
import java.util.List;

public class DingDaoImpl implements DingDao {
    QueryRunner queryRunner = new QueryRunner();
    /**
     * 添加交易订单
     *
     * @param ding
     * @return
     */
    @Override
    public int add(Ding ding) {
        int num = 0;
        try{
            String sql = "INSERT INTO ding(dingId,jId,coId,coPrice,mId,mCount,mPrice,sId,sPrice,bId,bPrice,uId,message,dingTime,dingSum,totalId)VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);";
            Object[] objects = new Object[]{
                    ding.getDingId(),
                    ding.getjId(),
                    ding.getCoId(),
                    ding.getCoPrice(),
                    ding.getmId(),
                    ding.getmCount(),
                    ding.getmPrice(),
                    ding.getsId(),
                    ding.getsPrice(),
                    ding.getbId(),
                    ding.getbPrice(),
                    ding.getuId(),
                    ding.getMessage(),
                    ding.getDingTime(),
                    ding.getDingSum(),
                    ding.getTotalId()
            };
            num = queryRunner.update(DruidUtils.getConnection(),sql,objects);
        }catch (Exception e){
            e.printStackTrace();
        }
        return num;
    }

    /**
     * 通过id修改订单详情
     *
     * @param ding
     * @return
     * @throws Exception
     */
    @Override
    public int update(Ding ding) throws Exception {
        int num = 0;
        try{
            String sql = "update ding set message = ? where dingId=?;";
            Object[] objects = new Object[]{
                    ding.getMessage(),
                    ding.getDingId()
            };
            num = queryRunner.update(DruidUtils.getConnection(),sql,objects);
        }catch (Exception e){
            e.printStackTrace();
        }
        return num;
    }

    /**
     * 查询所有订单
     *
     * @return
     */
    @Override
    public List<Ding> getAll() {
        List<Ding> dingList = new ArrayList<Ding>();
        try{
            String sql = "select * from ding;";
            dingList = queryRunner.query(DruidUtils.getConnection(),sql,new BeanListHandler<Ding>(Ding.class));
        }catch (Exception e){
            e.printStackTrace();
        }
        return dingList;
    }

    /**
     * 根据订单id查询金额
     *
     * @param dingId
     * @return
     */
    @Override
    public Ding getDingBydingId(Integer dingId) throws Exception {
        Ding ding = new Ding();
        try{
            String sql = "select dingSum from ding where dingId = ?;";
            ding = queryRunner.query(DruidUtils.getConnection(),sql,new BeanHandler<Ding>(Ding.class),dingId);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ding;
    }


}
